#include "com_voe_VoECallReport.h"
#include "jni_api.h"
#include "OutputParam.h"
#include "voe_call_report.h"

/*
 * Class:     com_voe_VoECallReport
 * Method:    GetInterface
 * Signature: (J)J
 */
JNIEXPORT jlong JNICALL Java_com_voe_VoECallReport_GetInterface
	(JNIEnv* env, jclass cls, jlong p_voiceEngine)
{
	return (jlong)webrtc::VoECallReport::GetInterface((webrtc::VoiceEngine*)p_voiceEngine);
}

/*
 * Class:     com_voe_VoECallReport
 * Method:    Release
 * Signature: (J)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoECallReport_Release
	(JNIEnv* env, jclass cls, jlong p)
{
	webrtc::VoECallReport* pVoECallReport = (webrtc::VoECallReport*)p;
	return pVoECallReport->Release();
}

/*
 * Class:     com_voe_VoECallReport
 * Method:    ResetCallReportStatistics
 * Signature: (JI)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoECallReport_ResetCallReportStatistics
	(JNIEnv* env, jclass cls, jlong p, jint channel)
{
	webrtc::VoECallReport* pVoECallReport = (webrtc::VoECallReport*)p;
	return pVoECallReport->ResetCallReportStatistics(channel);
}

/*
 * Class:     com_voe_VoECallReport
 * Method:    GetEchoMetricSummary
 * Signature: (JLcom/voe/common/OutputParam;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoECallReport_GetEchoMetricSummary
	(JNIEnv* env, jclass cls, jlong p, jobject obj)
{
	webrtc::VoECallReport* pVoECallReport = (webrtc::VoECallReport*)p;
	webrtc::EchoStatistics stats;
	jint result = pVoECallReport->GetEchoMetricSummary(stats);
	if(result == 0)
	{
		JniOutputBuilder job(env);
		OutputParam param(env, obj);
		param.setArray(job
			.add(stats.erl.min).add(stats.erl.max).add(stats.erl.average)
			.add(stats.erle.min).add(stats.erle.max).add(stats.erle.average)
			.add(stats.rerl.min).add(stats.rerl.max).add(stats.rerl.average)
			.add(stats.a_nlp.min).add(stats.a_nlp.max).add(stats.a_nlp.average)
			.getOutput());
	}
	return result;
}

/*
 * Class:     com_voe_VoECallReport
 * Method:    GetRoundTripTimeSummary
 * Signature: (JILcom/voe/common/OutputParam;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoECallReport_GetRoundTripTimeSummary
  (JNIEnv* env, jclass cls, jlong p, jint channel, jobject obj)
{
	webrtc::VoECallReport* pVoECallReport = (webrtc::VoECallReport*)p;
	webrtc::StatVal delaysMs;
	jint result = pVoECallReport->GetRoundTripTimeSummary(channel, delaysMs);
	if(result == 0)
	{
		JniOutputBuilder job(env);
		OutputParam param(env, obj);
		param.setArray(job
			.add(delaysMs.min).add(delaysMs.max).add(delaysMs.average)
			.getOutput());
	}
	return result;
}

/*
 * Class:     com_voe_VoECallReport
 * Method:    GetDeadOrAliveSummary
 * Signature: (JILcom/voe/common/OutputParam;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoECallReport_GetDeadOrAliveSummary
	(JNIEnv* env, jclass cls, jlong p, jint channel, jobject obj)
{
	webrtc::VoECallReport* pVoECallReport = (webrtc::VoECallReport*)p;
	int numOfDeadDetections, numOfAliveDetections;
	jint result = pVoECallReport->GetDeadOrAliveSummary(channel, numOfDeadDetections, numOfAliveDetections);
	if(result == 0)
	{
		JniOutputBuilder job(env);
		OutputParam param(env, obj);
		param.setArray(job.add(numOfDeadDetections).add(numOfAliveDetections).getOutput());
	}
	return result;
}

/*
 * Class:     com_voe_VoECallReport
 * Method:    WriteReportToFile
 * Signature: (JLjava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoECallReport_WriteReportToFile
  (JNIEnv* env, jclass cls, jlong p, jstring fileNameUTF8)
{
	webrtc::VoECallReport* pVoECallReport = (webrtc::VoECallReport*)p;
	return pVoECallReport->WriteReportToFile(JniHelper::j2c_string(env, fileNameUTF8).c_str());
}

